package com.yinhe.music.yhmusic.main.home;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusNullObject;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.RecommendMvAdapter;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceViewPagerFragment;
import com.yinhe.music.yhmusic.model.HomeInfo;
import com.yinhe.music.yhmusic.model.MvsInfo;
import com.yinhe.music.yhmusic.music.PlayUtils;
import com.yinhe.music.yhmusic.utils.UmengEventUtils;
import com.yinhe.music.yhmusic.widget.SpaceDecoration;

import java.util.List;

import butterknife.BindView;

public class MvFragment extends BaseServiceViewPagerFragment implements HomeContract.IHomeView,
        BaseQuickAdapter.OnItemClickListener{

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    private int totalPage;
    private int curPage = 1;
    private int pageSize = 10;
    private RecommendMvAdapter mvAdapter;

    HomePresenter mPresenter;

    public static MvFragment newInstance() {
        Bundle args = new Bundle();
        MvFragment fragment = new MvFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new HomePresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    public void initView() {
        RxBus.get().register(this);

        mvAdapter = new RecommendMvAdapter();
        mvAdapter.setOnItemClickListener(this);
        // 上拉加载
        mvAdapter.setOnLoadMoreListener(() -> {
            if (curPage <= totalPage) {
                mPresenter.getRecommendMvs(curPage, pageSize);
            } else {
                mvAdapter.loadMoreEnd();
            }
        }, mRecyclerView);
        mvAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);

        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2,
                GridLayoutManager.VERTICAL, false));
        mRecyclerView.addItemDecoration(new SpaceDecoration(getActivity(), 0, 0, 10 ,0));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setAdapter(mvAdapter);
    }

    @Override
    protected void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();

        mPresenter.loadCacheDataForHome();
        mPresenter.getRecommendMvs(1, pageSize);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home_list_mv;
    }

    @Override
    public void setRecommend(HomeInfo homeInfo) {
        List<MvsInfo> mMvList = homeInfo.getMovieList();
        this.totalPage = homeInfo.getPageNum();
        if (mMvList == null){
            return;
        }
        if (curPage == 1) {
            mvAdapter.setNewData(mMvList);
        }else {
            mvAdapter.addData(mMvList);
        }
        if (curPage >= totalPage) {
            mvAdapter.loadMoreEnd();
        }else {
            curPage++;
            mvAdapter.loadMoreComplete();
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        UmengEventUtils.clickRecommend(getActivity(), UmengEventUtils.MV);
        PlayUtils.getMvInfo(mvAdapter.getData().get(position).getMovieId(), getActivity());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.get().unregister(this);
    }

    /**
     * 切换语系
     *
     * @param rxbusNullObject {@link RxbusNullObject}
     */
    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = @Tag(RxBusEventType.Home.CHANGE_LANG_TYPE)
    )
    public void onRxbusChangeLang(RxbusNullObject rxbusNullObject) {
        curPage = 1;
        mPresenter.getRecommendMvs(curPage, pageSize);
    }

    /**
     * 网络状态改变
     *
     * @param isConnected 连接状态
     */
    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = @Tag(RxBusEventType.Home.NETWORK_CONNECT)
    )
    public void onRxbusNetworkStatusDidChanged(Boolean isConnected) {
//        if (isConnected && mIsNetworkError) {
//            mIsNetworkError = false;
//            mPresenter.getRecommendList();
//        } else {
//            mIsNetworkError = true;
//        }
    }
}
